import { combineReducers } from 'redux'
import * as ACT from './contants'
import indexReducer from '@pages/Index/store/reducer'
import { UTIL_BAR } from '@util/state'
import { LM_CLICK } from '@util/state.js'

const defaultValue = {
    headerHeight: 0,
    //header菜单
    leftNav: [
        {
            title: '主页',
            link: '/ararin-ui/home',
            active: false
        }
    ],
    rightNav: [
        {
            title: '组件',
            link: '/ararin-ui/view/Button',
            active: false
        },
        {
            title: '开发中2',
            link: '/ararin-ui/index',
            active: false
        }
    ],
    currentRouter: '',
    currentLeftMenu: {
        index: -1,
        sindex: -1
    },
    
    // 左侧菜单
    leftMenuData: [
        {
            clickType: LM_CLICK.MAIN_MENU,
            label: 'Common',
            children: [
                {
                    title: 'Button',
                    subTitle: '按钮',
                    link: '/ararin-ui/view/Button'
                },
                {
                    title: 'Icon',
                    subTitle: '图标',
                    link: '/ararin-ui/view/Icon'
                }
            ]
        },{
            clickType: LM_CLICK.MAIN_MENU,
            label: 'Dialog',
            children: [
                {
                    title: 'Dialog',
                    subTitle: '对话框',
                    link: '/ararin-ui/view/Dialog'
                },
                {
                    title: 'Notify',
                    subTitle: '提示',
                    link: '/ararin-ui/view/Notify'
                }
            ]
        }, {
            clickType: LM_CLICK.MAIN_MENU,
            label: 'Picker',
            children: [
                {
                    title: 'Picker',
                    subTitle: '选择器',
                    link: '/ararin-ui/view/Picker'
                },
                {
                    title: 'PickerView',
                    subTitle: '选择器滑动块列',
                    link: '/ararin-ui/view/PickerView'
                }, {
                    title: 'DatePicker',
                    subTitle: '时间选择器',
                    link: '/ararin-ui/view/DatePicker'
                }
            ]
        }, {
            clickType: LM_CLICK.MAIN_MENU,
            label: 'Data Entry',
            children: [
                {
                    title: 'Input',
                    subTitle: '输入框',
                    link: '/ararin-ui/view/Input'
                },
            ]
        }, {
            clickType: LM_CLICK.MAIN_MENU,
            label: 'Plugin',
            children: [
                {
                    title: 'PrizeWheel',
                    subTitle: '转盘抽奖',
                    link: '/ararin-ui/view/PrizeWheel'
                },
                {
                    title: 'PrizeSudoku',
                    subTitle: '转九宫格抽奖',
                    link: '/ararin-ui/view/PrizeSudoku'
                },
                {
                    title: 'ScratchCard',
                    subTitle: '刮刮乐',
                    link: '/ararin-ui/view/ScratchCard'
                }
            ]
        }
    ],
    // 工具栏状态
    utilData: [
        {
            title: '模拟器',
            active: false,
            icon: '<svg t="1607274776869" className="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9389"><path d="M736 64H288a71.76 71.76 0 0 0-71.68 71.68v752.64A71.76 71.76 0 0 0 288 960h448a71.76 71.76 0 0 0 71.68-71.68V135.68A71.76 71.76 0 0 0 736 64zM512 879.36a35.84 35.84 0 1 1 35.84-35.84A35.84 35.84 0 0 1 512 879.36z m233-116.48H279V189.44h466z" p-id="9390"></path></svg>',
            // svgColor: '#FF8C00',
            bgColor: '#55bb8a'
        }
    ],
    phoneConfig: [
        {
            iPhone6: {
                width: 375,
                height: 667
            }
        }
    ],
    activePhone: {
        width: 375,
        height: 667
    },
    position: {
        x: '',
        y: ''
    }
}

const mainReducer = (state = defaultValue, action) => {
    let prevState = JSON.parse(JSON.stringify(state))

    switch (action.type) {
        case ACT.TEST:
            return {...state, testData: action.value}
        // 头部组件菜单高亮
        case ACT.SET_HEADER_HEIGHT: 
            return {...prevState, headerHeight: action.val}
        case ACT.SET_HEDER_NAV:
            console.log(action)
            let leftNav = prevState.leftNav.map(item => ({...item, active: false}))
            let rightNav = prevState.rightNav.map(item => ({...item, active: false}))
            if(action.val.type === 'left') {
                leftNav[action.val.index].active = true
            }else if(action.val.type === 'right') {
                rightNav[action.val.index].active = true
            }
            return {...prevState, leftNav, rightNav}
        case ACT.SET_UTIL_BAR:
            let { utilData } = prevState
            utilData[action.val].active = !utilData[action.val].active
            return {...prevState, utilData}
        case ACT.SET_PHONE_POSITION: 
            return {
                ...prevState, 
                position: {
                    x: action.val.x,
                    y: action.val.y
                }
            }
        case ACT.SET_CURRENT_LEFT_MENU:
            
            const cmp = action.val
            const { leftMenuData } = prevState
            let currentLeftMenu = {
                index: -1,
                sindex: -1
            }

            leftMenuData.forEach((item, index) => {
                item.children.forEach((sitem, sindex) => {
                    if(sitem.title === cmp) {
                        currentLeftMenu['index'] = index
                        currentLeftMenu['sindex'] = sindex
                    }
                })
            })

            return {
                ...prevState,
                currentLeftMenu
            } 
        default:
            return prevState
    }
}

const reducer =  combineReducers({
    index: indexReducer,
    main: mainReducer
})

export default reducer